﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using viNews.Classes;
using viNews.Providers;
using visinia.SmartEngine.Cache;
using System.Data;

namespace viNews.Controllers
{
    public class NewsAccessHelper
    {
        #region DATA ACCESS HELPERS
        /// <summary>
        /// Gets the latest news
        /// </summary>
        /// <param name="count">The number of news to return</param>
        /// <returns>List of news</returns>
        public static List<News> GetNews(int count)
        {
            return NewsController.FillNewsList(viNewsDataProvider.Provider.GetNews(0, count, true));
        }

        /// <summary>
        /// Gets the total news count
        /// </summary>
        /// <returns>Count of all news</returns>        
        public static int GetNewsCount()
        {
            return NewsController.GetNewsCount();            
        }

        /// <summary>
        /// Gets the latest news of the specified category
        /// </summary>
        /// <param name="category">The category whose news to return</param>
        /// <param name="Count">The number of news to return</param>
        /// <returns>List of news</returns>
        public static List<News> GetNews(Category category, int Count)
        {
            return NewsController.FillNewsList(viNewsDataProvider.Provider.GetNewsByCategory(category.CategoryId, Count));
        }

        /// <summary>
        /// Gets the count of all news under this specified category
        /// </summary>
        /// <param name="category">The category whose count of news to return</param>        
        public static int GetNewsCount(Category category)
        {
            if (DataCache.GetCache("newsCountByCategory") == null)
            {
                int count = 0;
                IDataReader dr = viNewsDataProvider.Provider.GetNewsCountByCategory(category.CategoryId);
                if (dr.Read())
                {
                    if (!dr.IsDBNull(0))
                    {
                        count = dr.GetInt32(0);
                    }
                }
                dr.Close();
                DataCache.SetCache("newsCountByCategory", count, new TimeSpan(0, 20, 0));
            }
            return (int)DataCache.GetCache("newsCountByCategory");
        }

        /// <summary>
        /// Gets the latest news of the specified tag
        /// </summary>
        /// <param name="tag">The tag whose news to return</param>
        /// <param name="Count">The number of news to return</param>
        /// <returns>List of news</returns>
        public static List<News> GetNews(Tag tag, int Count)
        {
            return NewsController.FillNewsList(viNewsDataProvider.Provider.GetNewsByTag(tag.TagId, Count));
        }

        /// <summary>
        /// Gets the count of all news under this specified tag
        /// </summary>
        /// <param name="tag">The tag whose count of news to return</param>        
        public static int GetNewsCount(Tag tag)
        {
            if (DataCache.GetCache("newsCountByTag") == null)
            {
                int count = 0;                
                IDataReader dr = viNewsDataProvider.Provider.GetNewsCountByTag(tag.TagId);
                if (dr.Read())
                {
                    if (!dr.IsDBNull(0))
                    {
                        count = dr.GetInt32(0);
                    }
                }
                dr.Close();
                DataCache.SetCache("newsCountByTag", count, new TimeSpan(0, 20, 0));
            }
            return (int)DataCache.GetCache("newsCountByTag");
        }

        /// <summary>
        /// Gets the news specified in a comma separated list
        /// </summary>
        /// <param name="NewsList">The news list</param>
        /// <param name="Count">The number of news to return</param>
        /// <returns>List of news</returns>
        public static List<News> GetNews(string NewsList, int Count)
        {
            if (string.IsNullOrEmpty(NewsList))
                return null;
            List<News> list = new List<News>();
            string[] arr = NewsList.Split(',');
            foreach (string newsid in arr)
            {
                if (string.IsNullOrEmpty(newsid))
                    continue;
                News news = NewsController.GetNews(Convert.ToInt32(newsid));
                if (news != null)
                {
                    list.Add(news);
                }
            }
            if (list.Count < Count)
                Count = list.Count;
            return list.GetRange(0, Count);
        }
                
        /// <summary>
        /// Gets the count of all news in the specified news list 
        /// </summary>
        /// <param name="NewsList">The comma separated list of news</param>        
        public static int GetNewsCount(string NewsList)        
        {
            if (string.IsNullOrEmpty(NewsList))
                return 0;
            List<News> list = new List<News>();
            string[] arr = NewsList.Split(',');            
            foreach (string newsid in arr)
            {
                if (string.IsNullOrEmpty(newsid))
                    continue;
                News news = NewsController.GetNews(Convert.ToInt32(newsid));
                if (news != null)
                {
                    list.Add(news);
                }
            }
            return list.Count;            
        }
        #endregion
    }
}
